<?php

/**
 * This is the model class for table "{{setting}}".
 *
 * The followings are the available columns in table '{{setting}}':
 * @property string $name
 * @property string $value
 * @property string $isactive
 * @property integer $createtime
 * @property integer $updatetime
 */
class Setting extends CDbActiveRecord {
	protected $cacheKey = 'yii::cache.{{db}}.{{table}}';
	
	// protected $cacheId = 'memcache';
	private $_enableCaching = true;
	private $_caching = array (
			'cacheId' => 'memcache',
			'class' => 'system.caching.CMemCache',
			'expire' => 6400 
	);
	public $alias = 'cd';
	
	// public $criteria;
	
	/**
	 * Returns the static model of the specified AR class.
	 * 
	 * @param string $className
	 *        	active record class name.
	 * @return Setting the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model ( $className );
	}
	
	/**
	 *
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{setting}}';
	}
	
	/**
	 *
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array (
				array (
						'name, value',
						'required' 
				),
				array (
						'createtime, updatetime',
						'numerical',
						'integerOnly' => true 
				),
				array (
						'name',
						'length',
						'max' => 20 
				),
				array (
						'isactive',
						'length',
						'max' => 1 
				),
				// The following rule is used by search().
				// Please remove those attributes that should not be searched.
				array (
						'name, value, isactive, createtime, updatetime',
						'safe',
						'on' => 'search' 
				) 
		);
	}
	
	/**
	 *
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array ();
	}
	
	/**
	 *
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array (
				'name' => 'Name',
				'value' => 'Value',
				'isactive' => 'Isactive',
				'createtime' => 'Createtime',
				'updatetime' => 'Updatetime' 
		);
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * 
	 * @return CActiveDataProvider the data provider that can return the models
	 *         based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria = new CDbCriteria ();
		
		$criteria->compare ( 'name', $this->name, true );
		$criteria->compare ( 'value', $this->value, true );
		$criteria->compare ( 'isactive', $this->isactive, true );
		$criteria->compare ( 'createtime', $this->createtime );
		$criteria->compare ( 'updatetime', $this->updatetime );
		
		return new CActiveDataProvider ( $this, array (
				'criteria' => $criteria 
		) );
	}
	public function defaultScope() {
		// return parent::defaultScope();
		/*return array (
				'alias' => $this->alias,
				'condition' => $this->alias . '.isactive=1' 
		);*/
		
		return CMap::mergeArray(
				parent::defaultScope(),array(
				'alias'=>$this->alias)
		);
	}
	public function scopes() {
		return array ();
	}
	
	/**
	 * (non-PHPdoc)
	 * @see CActiveRecord::resetScope()
	 */
	/*public function resetScope(){
		$this->_c=new CDbCriteria();
		return $this;
	}*/
	
	/**
	 * (non-PHPdoc)
	 * @see CActiveRecord::resetScope()
	 */
	/*public function resetScope(){
		if(null!==($criteria=$this->getDbCriteria(false))){
			foreach(get_class_vars('CDbCriteria') as $k=>$v)
				$criteria->$k=$v;
		}
		return $this;
	}*/
	/**
	 * self::model()->recently(5)->findAll();
	 * 
	 * @param  $limit
	 * @return Setting
	 */
	public function recently($limit = 5){
		$this->getDbCriteria()->mergeWith(array(
				'order'=>'create_time DESC',
				'limit'=>$limit,
		));
		return $this;
	}
	
	protected function afterFind() {
		// echo $this->value;
		// var_dump(json::decode($this->value));
		// echo '<pre>';
		// $this->Owner->safeAttributeNames();
		// print_r($this->Owner->getSafeAttributeNames());
		// print_r($this->getSafeAttributeNames());
		$d = json::decode ( $this->value );
		foreach ( $d as $key => $value ) {
			// $this->value->$key = $value;
		}
		// return parent::afterFind();
	}
	public function __set($name, $value) {
		if (! in_array ( $name, $this->getSafeAttributeNames () )) {
			// echo '<BR>' . $name . ':' . $value;
			$this->$name = $value;
		}
		parent::__set ( $name, $value );
	}
	public function __get($name) {
		return parent::__get ( $name );
	}
}